/**
 * api-add
 */
function article_save() {
    alert("刷新父级的时候会自动关闭弹层。")
    // window.parent.location.reload();
}
$(function () {
    $('.skin-minimal input').iCheck({
        checkboxClass: 'icheckbox-blue',
        radioClass: 'iradio-blue',
        increaseArea: '20%'
    });
    allSubject();
    //格式化返回结果
    formatResult();

});

/* 格式化返回结果 */
function formatResult() {
    $("#apiResult").mouseout(function () {
        var result = $('#apiResult').val();
        $("#apiResult").val(JSON.stringify(JSON.parse($.trim(result)), null, 4));
        console.log(JSON.stringify(JSON.parse(result), null, 4));
    });
}

/* 新增参数 */
function addParams() {
    var tr = $("#paramsTable tr").eq(1).clone();
    tr.appendTo("#paramsTable");
}

/*参数-删除*/
function api_del(obj, id) {

    $(obj).parents("tr").remove();
    layer.msg('已删除!', {icon: 1, time: 1000});
    /*	layer.confirm('确认要删除吗？',function(index){
            $(obj).parents("tr").remove();
            layer.msg('已删除!',{icon:1,time:1000});
        });*/
}


/* 新增项目 */
function addSubject() {
    //prompt层
    layer.prompt({title: '新增项目，并确认', formType: 3}, function (text, index) {

        var params = {
            subjectName: text
        }
        $.post('/back/apiSubject/add', params, function (data) {
            if (data.status) {
                layer.msg('提交成功!', {icon: 1, time: 2000});
                var options = '';
                options = "<option value='" + data.object + "'>" + text + "</option>";
                $('#selSubject').append(options);
                layer.close(index);
            } else {
                layer.msg('提交失败!', {icon: 2, time: 2000});
            }
        });

    });
}

/* 新增分类 */
function addType() {
    //prompt层
    layer.prompt({title: '新增分类，并确认', formType: 3}, function (text, index) {

        var params = {
            apiType: text,
            subjectId: $('#selSubject').val()
        }
        $.post('/back/apiType/add', params, function (data) {
            if (data.status) {
                layer.msg('提交成功!', {icon: 1, time: 2000});
                var options = '';
                options = "<option value='" + data.object + "'>" + text + "</option>";
                $('#selType').append(options);
                layer.close(index);
            } else {
                layer.msg('提交失败!', {icon: 2, time: 2000});
            }
        });

    });
}

/*加载所有项目*/
function allSubject() {

    $.post('/back/apiSubject/list', {}, function (data) {
        if (data.status) {
            for (var i = 0; i < data.list.length; i++) {
                var options = '';
                options = "<option value='" + data.list[i].id + "'>" + data.list[i].subjectName + "</option>";
                $('#selSubject').append(options);
            }
            listType();
            $("#selSubject").bind('change', function () {
                listType();
            });

        }
    });
}

/* 根据项目得到分类列表 */
function listType() {
    var params = {
        subjectId: $('#selSubject').val()
    }
    $.post('/back/apiType/list', params, function (data) {
        $('#selType').empty();
        if (data.status) {
            for (var i = 0; i < data.list.length; i++) {
                var options = '';
                options = "<option value='" + data.list[i].id + "'>" + data.list[i].apiType + "</option>";
                $('#selType').append(options);
            }
        }
    });
}

/* 保存 */
function api_save() {

    var param = {};
    //接口名称
    var apiName = $('#api_name').val();
    //接口地址
    var apiUrl = $('#api_url').val();
    //api所属项目
    var apiSubjectId = $('#selSubject').val();
    //api类型id
    var apiTypeId = $('#selType').val();
    //api请求方式
    var apiRequestType = $('#requestType').val();
    //api返回结果示例
    var apiResult = $('#apiResult').val();

    param.apiName = apiName;
    param.apiUrl = apiUrl;
    param.apiSubjectId = apiSubjectId;
    param.apiTypeId = apiTypeId;
    param.apiRequestType = apiRequestType;
    param.apiResult = apiResult;

    var params = "[";
    var total = $('#paramsTable tr').length;

    $('#paramsTable tr').each(function (idx, data) {
        if (idx == 0) return;

        $(this).children("td").each(function (i) {
            switch (i) {
                case 0:
                    params += '{"paramName":"' + $(this).find('input').eq(0).val() + '",';
                    break;
                case 1:
                    params += '"paramType":"' + $(this).find('select').eq(0).val() + '",';
                    break;
                case 2:
                    params += '"paramBoolean":"' + $(this).find('select').eq(0).val() + '",';
                    break;
                case 3:
                    if ((idx + 1) == total) {
                        params += '"paramRemark":"' + $(this).find('input').eq(0).val() + '"}';
                    } else {
                        params += '"paramRemark":"' + $(this).find('input').eq(0).val() + '"},';
                    }
                    break;
                default:
            }

        });

    });
    params += "]";
    param.params = params;
    console.log(params);

    $.post('/back/api/add', param, function (data) {
        if (data.status) {
            layer.msg("新增成功");
        }
    });
}